#!/bin/bash

echo "=== 验证用户报告的Bug ==="

# Function to run SQL command and show result
run_sql() {
    echo ">>> 执行: $1"
    ./bin/gu_new -h localhost -P 7777 -u admin "$1" 2>&1
    echo ""
}

echo "测试1: 没有选择数据库时SHOW TABLES应该看不到表"
echo "---"
run_sql "SHOW TABLES;"

echo "测试2: 创建数据库并测试表字段重复问题"
echo "---"
run_sql "CREATE DATABASE testbug;"
run_sql "USE testbug;"
run_sql "SHOW TABLES;"

echo "测试3: 创建表，检查字段"
echo "---"
run_sql "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50));"
run_sql "DESCRIBE users;"

echo "测试4: 删除表并重新创建，检查字段是否叠加"
echo "---"
run_sql "DROP TABLE users;"
run_sql "CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(100));"  
run_sql "DESCRIBE users;"

echo "测试5: 再次删除并创建相同表，检查字段叠加"
echo "---"
run_sql "DROP TABLE users;"
run_sql "CREATE TABLE users (id INT PRIMARY KEY, phone VARCHAR(20));"
run_sql "DESCRIBE users;"

echo "测试6: 测试字段重复添加"
echo "---"
run_sql "CREATE TABLE test_dup (id INT, id INT, name VARCHAR(50));"
run_sql "DESCRIBE test_dup;"

echo "测试7: 测试主键约束和INSERT"
echo "---"
run_sql "CREATE TABLE pk_test (id INT PRIMARY KEY, value VARCHAR(50));"
run_sql "INSERT INTO pk_test VALUES (1, 'first');"
run_sql "INSERT INTO pk_test VALUES (1, 'duplicate');"  # 应该失败
run_sql "SELECT * FROM pk_test;"

echo "测试8: 测试UPDATE功能"
echo "---"
run_sql "UPDATE pk_test SET value = 'updated' WHERE id = 1;"
run_sql "SELECT * FROM pk_test;"

echo "测试9: 测试ALTER功能"
echo "---"
run_sql "ALTER TABLE pk_test ADD COLUMN description VARCHAR(100);"
run_sql "DESCRIBE pk_test;"

echo "=== Bug验证测试完成 ==="